package cn.selfiot.iotman;

import cn.selfiot.iotman.core.I18N;
import cn.selfiot.iotman.core.StageController;
import cn.selfiot.iotman.core.StageManager;
import cn.selfiot.iotman.helper.MessageDialogHelper;
import cn.selfiot.iotman.helper.ProjectPersistHelper;
import cn.selfiot.iotman.model.mqtt.ProjectModel;
import cn.selfiot.iotman.view.ResourceConstants;
import cn.selfiot.iotman.view.project.MqttProjectController;
import cn.selfiot.iotman.view.projectitem.ProjectItem;
import com.panemu.tiwulfx.dialog.MessageDialog;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author hugo
 * @since 2020/7/29
 */
public class MainController extends StageController {
    private static final Logger LOGGER = LoggerFactory.getLogger(MainController.class);
    
    @FXML
    public VBox pane;
    
    @FXML
    public HBox header;
    
    @FXML
    public HBox footer;
    
    @FXML
    public FlowPane projects;
    
    @FXML
    public VBox vbAddNew;
    
    @FXML
    public Button addNew;
    
    @FXML
    public Button btnExit;
    
    @FXML
    public TextField txtSearch;
    
    @FXML
    public Button btnSearch;
    
    @FXML
    public Hyperlink more;
    
    private StageManager stageManager;
    private ResourceBundle bundle;
    private List<ProjectModel> projectList;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        stageManager = StageManager.getInstance();
        bundle = I18N.getBundle();
        
        header.prefWidthProperty().bind(pane.widthProperty());
        footer.prefWidthProperty().bind(pane.widthProperty());
        projects.prefWidthProperty().bind(pane.widthProperty());
        
        addNew.setOnAction(actionEvent -> newProjectView());
        btnExit.setOnAction(actionEvent -> exit());
        more.setOnAction(actionEvent -> openSite());
        txtSearch.setOnKeyPressed(keyEvent -> {
            if (keyEvent.getCode().equals(KeyCode.ENTER)) {
                projectFilter();
            }
        });
        btnSearch.setOnAction(actionEvent -> projectFilter());
        vbAddNew.setOnMouseClicked(actionEvent -> newProjectView());
        
        Platform.runLater(() -> {
            getData();
            loadProjects();
        });
    }
    
    private void loadProjects() {
        this.projects.getChildren().clear();
        for (ProjectModel project : projectList) {
            ProjectItem item = new ProjectItem(this.getStage(), project);
            this.projects.getChildren().add(item);
        }
        this.projects.getChildren().add(vbAddNew);
    }
    
    private void projectFilter() {
        Platform.runLater(() -> {
            String keyword = txtSearch.getText();
            
            this.projectList = ProjectPersistHelper
                    .findByName(this.projectList, keyword);
            loadProjects();
        });
    }
    
    @FXML
    private void newProjectView() {
        Stage stage = new Stage();
        MqttProjectController controller = new MqttProjectController();
        controller.setStage(stage);
        this.stageManager.loadStage(
                stage,
                controller,
                ResourceConstants.PROJECT_VIEW,
                ResourceConstants.PROJECT_RES,
                I18N.get("project.title"),
                Modality.APPLICATION_MODAL
        ).show();
    }
    
    @FXML
    public void exit() {
        MessageDialog.Answer answer = MessageDialogHelper.confirm(this.getStage(),
                bundle.getString("dialog.exit" +
                        ".message"));
        if (answer == MessageDialog.Answer.YES_OK) {
            this.getStage().close();
        }
    }
    
    @FXML
    private void openSite() {
        LOGGER.info("open site ...");
    }
    
    private void getData() {
        this.projectList = ProjectPersistHelper.getProjects();
    }
    
    @Override
    public void callback() {
        Platform.runLater(() -> {
            getData();
            loadProjects();
        });
    }
}
